<?php
class SleekBase_SkipNavigationModule extends Module {
	public function run () {
		if (
			($page = SleekPHP::currentPage()) and 
			($links = $this->extractSkipLinks($page))
		) {
			$this->setData('links', $links);
		}
		else {
			$this->setView(false);
		}
	}

	private function extractSkipLinks ($page) {
		$path			= SleekPHP::site()->getPagePath($page);
		$matches		= array();
		$contents		= file_get_contents($path);
		$links			= array();

		preg_match_all('/<wrapper.*?name="(.*?)".*?>/im', $contents, $matches);

		if (stristr($contents, '<Navigation')) {
			$links[] = array(
				'title'	=> 'Navigation', 
				'url'	=> '#navigation'
			);
		}

		foreach ($matches[1] as $wrapper) {
			if ($wrapper != 'wrapper') {
				$links[] = array(
					'title'	=> ucfirst(str_replace('-', ' ', $wrapper)), 
					'url'	=> '#' . $wrapper
				);
			}
		}

		return count($links) ? $links : false;
	}
}
